package com.yoomiito.app.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String ms2min(String ms) {
        int time = Integer.parseInt(ms);
        int s0 = time / 1000;
        int min = s0 / 60;
        int s = s0 % 60;
        return addZero(min) + ":" + addZero(s);
    }

    //十位时间戳字符串转小时分秒
    public static String Hourmin(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("HH:mm:ss");
//        @SuppressWarnings("unused")
//        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    public static String minSecond(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("mm:ss");
//        @SuppressWarnings("unused")
//        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    public static String Hourmin(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat("HH.mm.ss");
        String times = sdr.format(new Date(time * 1000L));
        return times;
    }

    //十位时间戳字符串转年月
    public static String YearMon(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    //十位时间戳字符串转月日
    public static String MonthDay(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    //倒计时转天时分秒
    public static String[] stamp2time(long time) {
        int lastTime, d, h, m, s;
        int daysTime = 60 * 60 * 24;
        d = (int) (time / daysTime);
        lastTime = (int) (time % daysTime);
        h = lastTime / (60 * 60);
        lastTime = lastTime % (60 * 60);
        m = lastTime / 60;
        s = lastTime % 60;
        return new String[]{addZero(d), addZero(h), addZero(m), addZero(s)};
    }

    private static String addZero(int i) {
        return i < 10 ? ("0" + i) : String.valueOf(i);
    }

    /******************************************************************************/

    /*
     * 将时间戳转换为时间
     *
     * s就是时间戳
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //如果它本来就是long类型的,则不用写这一步
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static String current() {
        Calendar calendar = Calendar.getInstance();
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH)+1;
        int d = calendar.get(Calendar.DAY_OF_MONTH);
        int h = calendar.get(Calendar.HOUR);
        int min = calendar.get(Calendar.MINUTE);
        int s = calendar.get(Calendar.SECOND);
        int ms = calendar.get(Calendar.MILLISECOND);
        return ""+y+addZero(m)+addZero(d)+addZero(h)+addZero(min)+addZero(s)+ms;
    }
    public static String currentYear() {
        Calendar calendar = Calendar.getInstance();
        int y = calendar.get(Calendar.YEAR);

        return ""+y;
    }



    public static String currentDate() {
        Calendar calendar = Calendar.getInstance();
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH)+1;
        int d = calendar.get(Calendar.DAY_OF_MONTH);
        return ""+y+addZero(m)+addZero(d);
    }

    public static String currentTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

}
